import { connect } from 'umi';
import { ContractCountContext } from '../common';
import { Spin, message } from 'antd';
import { reportStateByStepService } from '../services';
import { CountSearchFormView } from './CountSearchFormView';
import { useMemo, useState } from 'react';
import { StateCountEchartsView } from './StateCountEchartsView';
import { getLoadMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { contractStateData } from '../mconfig';

const StateCountManageView: React.FC<ContractAllCountAPI.IManageType> = ({ curRes }) => {
  /** 样式根目录 */
  const styleFixCls = 'contract-report-';

  /** 统计步长 */
  const [countStepType, setCountStepType] = useState<string>('月');

  /**  获取数据字典 */
  const contractStateStr: string = `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}`;
  const { loadMapInfo, mapLoading } = getLoadMapList([
    contractStateStr, //合同状态
  ]);

  /** 获取数据回调 */
  const allCountCallback = (res: any) => {
    if (res && res?.msgFlag) {
    } else {
      message.error(res?.msg);
    }
  };

  /** 引入接口 */
  const { reportStateByStepRun, reportStateByStepInfo, reportStateByStepLoading } = reportStateByStepService(
    curRes,
    allCountCallback,
  );

  /** 处理数据字典 */
  const loadInfoMemo = useMemo(() => {
    if (loadMapInfo[contractStateStr]?.length > 0) {
      return contractStateData(loadMapInfo[contractStateStr]);
    } else {
      return {};
    }
  }, [loadMapInfo[contractStateStr]]);

  return (
    <ContractCountContext.Provider
      value={{
        styleFixCls,
        reportByStepRun: reportStateByStepRun,
        echartsDetailInfo: reportStateByStepInfo,
        stepTypeValue: countStepType,
      }}
    >
      <Spin spinning={reportStateByStepLoading || mapLoading}>
        <div className={`${styleFixCls}allCountDiv`}>
          <CountSearchFormView countType="stateCount" changeStepType={setCountStepType} />
          <StateCountEchartsView contractStateInfo={loadInfoMemo} />
        </div>
      </Spin>
    </ContractCountContext.Provider>
  );
};
export default connect(({ baseLogin }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(StateCountManageView);
